package control;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.ImageFilter;

public class ProFrontController extends HttpServlet {
	
	
	@Override
	public void init() throws ServletException {	
		super.init();		
	}
	private void execute(HttpServletRequest req, HttpServletResponse resp) {

		String uri = req.getRequestURI();
		String contextPath = req.getContextPath();
		String command = uri.substring(contextPath.length());
		String sub = req.getParameter("sub");
		System.out.println("command:" + command + ", sub:" + sub);

		try {
			req.setAttribute("body", "studyContent.jsp");
			if (command.equals("/product.pro")) {	
				req.setAttribute("aside", "aside/productSidebar.jsp");
				req.setAttribute("body", "product/portfolio.jsp");
				if (sub == null) {
					req.setAttribute("body", "product/portfolio.jsp");
				} else if (sub.equals("crane")) {
					String path="imgs/woojin/crane/craneAssyWork/";
					String realPath=req.getRealPath(path);
					System.out.println("path:"+realPath); 
					String[] fileArray=new File(realPath).list(new ImageFilter());
					req.setAttribute("list", fileArray);
					req.setAttribute("path", path);
				} else if (sub.equals("cranePart")) {
					String path="imgs/woojin/crane/craneAssyWork/";
					String realPath=req.getRealPath(path);
					System.out.println("path:"+realPath); 
					String[] fileArray=new File(realPath).list(new ImageFilter());
					req.setAttribute("list", fileArray);
					req.setAttribute("path", path);
				} else if (sub.equals("brandDore")) {

				} else if (sub.equals("soundproof")) {

				} else if (sub.equals("elevatorPart")) {
					String path="imgs/woojin/elevatorPart/";
					String realPath=req.getRealPath(path);
					System.out.println("path:"+realPath); 
					String[] fileArray=new File(realPath).list(new ImageFilter());
					req.setAttribute("list", fileArray);
					req.setAttribute("path", path);
				} 
				else if (sub.equals("excavator")) {
					
				} else if (sub.equals("constructionMachinery")) {

				} else if (sub.equals("architectural")) {

				} else if (sub.equals("eco")) {

				}
				req.getRequestDispatcher("home.jsp").forward(req, resp);				
			} 		
			else {
				req.setAttribute("body", "template/404.jsp");
				req.getRequestDispatcher("home.jsp").forward(req, resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		execute(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		execute(req, resp);
	}
}

